package quant.smart.crawl.cmd.lme;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.alibaba.fastjson.JSON;

import quant.smart.crawl.cmd.BaseCommand;
import quant.smart.crawl.cmd.ClientContext;
import quant.smart.crawl.model.Inventory;
import quant.smart.crawl.model.SmartResponse;
import quant.smart.crawl.utils.Constants;
import quant.smart.crawl.utils.HttpUtil;
import quant.smart.crawl.utils.PrimaryUtil;

public class OmetalCmd extends BaseCommand {
	final Log logger=LogFactory.getLog(getClass());
	
	public OmetalCmd(ClientContext client) {
		super(client);
	}

	@Override
	public Object call() throws Exception {
		
		SmartResponse res=new SmartResponse(Constants.RETURN_SUCCESS);
		List<Inventory> list = new ArrayList<Inventory>();
		String url ="http://www.ometal.com/zhuanti_l/lmekucun/";		
		try {
			Map<String, String> header=new HashMap<String, String>();
			header.put("Encoding", "gbk");
			String html=HttpUtil.get(url, header,null);
			Document doc = Jsoup.parse(html);
			Element table = doc.select("table").get(1);
			String content =table.html();
			Inventory vo=new Inventory(PrimaryUtil.createDateId("LMECU"),"LME铜","LMECU");
			setInventory(vo,content,"铜库存");			
			list.add(vo);
			vo=new Inventory(PrimaryUtil.createDateId("LMEAL"),"LME铝","LMEAL");
			setInventory(vo,content,"铝库存");
			list.add(vo);
			vo=new Inventory(PrimaryUtil.createDateId("LMEZN"),"LME锌","LMEZN");
			setInventory(vo,content,"锌库存");
			list.add(vo);
			vo=new Inventory(PrimaryUtil.createDateId("LMEPB"),"LME铅","LMEPB");
			setInventory(vo,content,"铅库存");
			list.add(vo);
			vo=new Inventory(PrimaryUtil.createDateId("LMENI"),"LME镍","LMENI");
			setInventory(vo,content,"镍库存");
			list.add(vo);
			vo=new Inventory(PrimaryUtil.createDateId("LMESN"),"LME锡","LMESN");
			setInventory(vo,content,"锡库存");
			list.add(vo);
			res.setData(list);
			logger.info("LME:"+JSON.toJSONString(res));
		} catch (Exception e) {
			logger.error("error",e);
			res.setCode(Constants.RETURN_FAIL);
			res.setMessage(e.getMessage());
		}
		return res;
	}
	
	private void setInventory(Inventory vo,String content,String key){
		int n1 =content.indexOf(key);
		String change=content.substring(n1+3, n1+5);		
		int n3 =content.indexOf("吨", n1+3);
		String changeNum=content.substring(n1+5,n3).replace(",", "");
		int n4 =content.indexOf("至", n1+2);
		int n5 =content.indexOf("吨", n4);
		String total=content.substring(n4+1,n5).replace(",", "");
		if(change.equals("减少")) vo.setIncrease(-1*Integer.parseInt(changeNum));
		if(change.equals("增加")) vo.setIncrease(Integer.parseInt(changeNum));
		vo.setInventory(Integer.parseInt(total));		
		SimpleDateFormat sFormat=new SimpleDateFormat("yyyyMMdd");
		vo.setTimesSn(sFormat.format(new Date()));
	}

	public static void main(String[] args) throws Exception {
		OmetalCmd cmd= new OmetalCmd(null);
		cmd.call();
	}
}
